package com.t.screenofftouch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;

public class CommonFunctions {
	private static  DevicePolicyManager devicePolicyManager;
	private static ComponentName mDeviceAdminSample;
	
	public static boolean isActiveAdmin(Context context) {
		mDeviceAdminSample = new ComponentName(context,DeviceAdminSampleReceiver.class);
		devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
		return devicePolicyManager.isAdminActive(mDeviceAdminSample);
	}
	
	 public static void UpdateAnotherApp(String packageNameMDM, String packageName, String key, String value, Context context)
     {
         try
         {
             //String anotherPrefName = Constants.PrefName + packageName;
             String appPath = String.format(Constants.pathSource, packageName, Constants.PrefName);
             String anotherAppPath = String.format(Constants.pathSource, packageNameMDM, Constants.PrefName);

             SharedPreferencesUtility.saveAnotherPreferences(key, value, context, Constants.PrefName);
             File anotherFilePath = new File(anotherAppPath);
             File appFilePath = new File(appPath);
             
             if( appFilePath.exists() && anotherFilePath.exists()){
            	 CopyFile(anotherFilePath, appFilePath);
             }
             
             String result = SharedPreferencesUtility.getStringAnotherPreferences(key, Constants.PackageNameMDM, context, Constants.PrefName);
             result = result + "," + value; 
             SharedPreferencesUtility.saveAnotherPreferences(key, result, context, Constants.PrefName);
             if(anotherFilePath.exists() && appFilePath.exists()){
            	 CopyFile(appFilePath, anotherFilePath);
             }
         }
         catch (Exception e)
         {
        	 e.printStackTrace();
         }
     }
	 
	 public static void UpdateRemoveSetting(String packageNameMDM, String packageName, String key, String value, Context context)
     {
         try
         {
             //String anotherPrefName = Constants.PrefName + packageName;
             String appPath = String.format(Constants.pathSource, packageName, Constants.PrefName);
             String anotherAppPath = String.format(Constants.pathSource, packageNameMDM, Constants.PrefName);

             SharedPreferencesUtility.saveAnotherPreferences(key, value, context, Constants.PrefName);
             File anotherFilePath = new File(anotherAppPath);
             File appFilePath = new File(appPath);
             
             if( appFilePath.exists() && anotherFilePath.exists()){
            	 CopyFile(anotherFilePath, appFilePath);
             }
             
             String result = SharedPreferencesUtility.getStringAnotherPreferences(key, Constants.PackageNameMDM, context, Constants.PrefName);
             if(result.contains(value)){
            	 result = result.replace("," + value,"");
             }
             SharedPreferencesUtility.saveAnotherPreferences(key, result, context, Constants.PrefName);
             if(anotherFilePath.exists() && appFilePath.exists()){
            	 CopyFile(appFilePath, anotherFilePath);
             }
         }
         catch (Exception e)
         {
        	 e.printStackTrace();
         }
     }
	 
	 public static void CopyFile(File src, File dst){
		 try {
			 FileInputStream inStream = new FileInputStream(src);
			 FileOutputStream outStream = new FileOutputStream(dst);
			 FileChannel inChannel = inStream.getChannel();
			 FileChannel outChannel = outStream.getChannel();
			 inChannel.transferTo(0, inChannel.size(), outChannel);
			 inStream.close();
			 outStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	 
	 public static boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
		    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
		        if (serviceClass.getName().equals(service.service.getClassName())) {
		            return true;
		        }
		    }
		    return false;
	 }
}
